package com.genius.demo.rabbitmqconsumer.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.genius.demo.rabbitmqconsumer.config.RedisUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Component
public class ModelMessageReceiver {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private RedisUtil redisUtil;

    @RabbitListener(queues = "topic.component")
    @RabbitHandler
    public void process(@Payload byte[] message) throws IOException {
        Map<String, String> componentMsg = this.objectMapper.readValue(message, Map.class);
        String key = componentMsg.get("modelKey");
        long start = Long.parseLong(componentMsg.get("start"));
        List components = redisUtil.lrange(key);
        System.out.println("message:" + componentMsg.toString());
        System.out.println("redis size:" + components.size());
//        redisUtil.del(key);
        System.out.println("time:" + (System.currentTimeMillis() - start));
    }
}
